﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using TrainingWorkshop.IDAL;
using System.Data.SqlClient;
using System.Transactions;

namespace TrainingWorkshop.DAL_EF
{
    public class CourseDetailRepository_EF : ICourseDetailRepository
    {
        public void Insert(int courseId, Model.Student s)
        {

            using (var db = new projectbEntities())
            {
                lock (this)
                {

                    CourseDetail cd = new CourseDetail();

                    cd.CourseId = courseId;
                    cd.StudentId = s.StudentId;
                    db.AddToCourseDetails(cd);

                    db.SaveChanges();




                }
            }
        }
        public void Delete(int courseId, Model.Student s)
        {
            using (var db = new projectbEntities())
            {


                var courseDetail = from cd in db.CourseDetails
                                   where cd.StudentId == s.StudentId
                                       && cd.CourseId == courseId
                                   select cd;
                try
                {
                    if (courseDetail.Count() != 0)
                    {
                        db.CourseDetails.DeleteObject(courseDetail.First());
                        db.SaveChanges();
                    }
                }
                catch (System.Data.OptimisticConcurrencyException ex)
                {

                    db.ObjectStateManager.ChangeObjectState(courseDetail.First(), System.Data.EntityState.Detached);
                    db.Refresh(System.Data.Objects.RefreshMode.ClientWins, courseDetail.First());

                }


            }

        }

    }
}
